<?php
namespace app\home\model;
use think\Model;
use think\Db;
class KnowsModel extends Model
{
    public function __construct()
    {
        parent::__construct();
    }
    //首页育儿知识显示
    public function getKnows(){
        $knows = Db::table('knows')
            ->alias(['knows'=>'k','knows_type'=>'t'])
            ->join('knows_type','t.id=k.knows_type_id')
            ->where('k.is_del=0 AND t.is_del=0')
            ->order('t.create_time desc')
            ->select();
        $count =Db::table('knows')
            ->alias(['knows'=>'k','knows_type'=>'t'])
            ->join('knows_type','t.id=k.knows_type_id')
            ->where('k.is_del=0 AND t.is_del=0')
            ->order('t.create_time desc')
            ->count();
//        echo $this->getLastSql();
//            dump($knows);
        $data =[];
        foreach($knows as $id){
            $thisId =$id['id'];
            $thisId =Db::table('knows')->where("knows_type_id = $thisId AND is_del = 0")->value('id');
            $data[] =$thisId;
        }
//        dump($data);
//        exit;
//        dump($count);
        $rows=[];
        foreach($data as $key=>$k){
            $rows[$key]['id'] =$k;
        }
        foreach($knows as $key=>$v){
            $rows[$key]['img_url'] =$v['album_url'];
            $rows[$key]['title'] =mb_substr($v['parenting_title'],0,10,'UTF-8');
            $rows[$key]['content'] =mb_substr($v['parenting_content'],0,14,'UTF-8');
        }
        return $rows;

    }

    //专题内容显示
    public function getKnowsSpecial(){
        $knowsSpecial = Db::table('knows')
                ->alias(['knows'=>'k','knows_type'=>'t'])
                ->field('k.id,k.parenting_title,k.parenting_content,k.album_url,k.knows_time,k.is_special,t.know_name')
                ->join('knows_type','t.id=k.knows_type_id')
                ->where('k.is_home = 1 AND k.is_del =0 AND t.is_del=0 AND k.is_special =1 AND t.status=1')->order('k.knows_time desc')->select();
        return $knowsSpecial;
    }

    public function getKonwsCount($id){
        $count = Db::table('knows_special_num')->where('knows_id',$id)->count();
        return $count;
    }

    //获取专题标题
    public function getSpecialName(){
        $rows =Db::table('knows_type')->where('is_del = 0 AND status = 1')->select();
//        echo $this->getLastSql();
//        exit;
        return $rows;
    }

    /**
     * @param $id获取专题详细信息
     */
    public function getKnowsInfomation($id){
        $knowsSpecial = Db::table('knows')->where("id=$id AND is_del =0")->find();
        return $knowsSpecial;
    }

    /**
     * 获取分类专题数据
     *
     */
    public function getKnowsMsg($id){
        $getKnowsMsg =Db::table('knows')
            ->alias('k')
            ->field('k.id,k.parenting_title,k.album_url,k.know_describe,k.knows_time,t.know_name')
            ->join('knows_type t','t.id=k.knows_type_id')
            ->where("k.knows_type_id =$id AND k.is_del = 0 AND t.is_del=0 AND t.status = 1")
            ->select();
        return $getKnowsMsg;
    }

    /**
     * 往次数表里面增加数据
     */

    public  function getCount($id,$ip){
        $data=[
            'knows_id'=>$id,
            'local_ip'=>$ip,
            'local_time'=>time()
        ];
        $msg = Db::table('knows_special_num')->where("knows_id=$id")->order('local_time desc')->find();
//        dump($msg);
        if(!empty($msg)&&$ip==$msg['local_ip']&&(time()-$msg['local_time'])>12*3600){
            Db::table('knows_special_num')->insert($data);
        }else if(!empty($msg)&&$ip!=$msg['local_ip']){
            Db::table('knows_special_num')->insert($data);
        }else if(empty($msg)){
            Db::table('knows_special_num')->insert($data);
        };
    }
}